# """单例装饰器"""

# from functools import wraps

# def singleton(cls):
#     """单例装饰器"""
#     _instance = {}
#     @wraps(cls)
#     def _get_instance(*args, **kw):
#         """通过闭包存储实例"""
#         if cls not in _instance:
#             _instance[cls] = cls(*args, **kw)
#         return _instance[cls]
#     return _get_instance


# @singleton
# class Test():
#     def __init__(self, *args, **kw):
#         pass


# test1 = Test()
# test2 = Test()

# assert test1 is test2, "不是单例"



"""单例装饰器"""

from functools import wraps

def singleton(cls):
    """单例装饰器"""
    cls.__new_original__ = cls.__new__
    _instance = {}
    @wraps(cls)
    def singleton_new(cls, *args, **kw):
        it = cls.__dict__.get('__it__')
        if it is not None:
            return it
    
    cls.__new__ = singleton_new
    cls.__init__original__ = cls.__init__
    cls.__init__ = object.__init__

    return cls


@singleton
class Test():
    def __new__(cls):
        return object.__new__(cls)
    def __init__(self, *args, **kw):
        pass


test1 = Test()
test2 = Test()

assert test1 is test2, "不是单例"

print(Test)  # <class '__main__.Test'>
print(type(Test))  # <class 'type'>
